package com.boatAnchorAssistant.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.boatAnchorAssistant.pojo.dto.role.UserRoleDto;
import com.boatAnchorAssistant.pojo.entity.UserDataDictionary;
import com.boatAnchorAssistant.service.mapper.UserDataDictionaryMapper;
import com.boatAnchorAssistant.service.repository.in.UserDataDictionaryRepository;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;
    import com.boatAnchorAssistant.pojo.dto.userDataDictionary.*;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* 【描 述】：
* 【环 境】：J2SE
* 【详 细】：
*
*/
@Service
public class UserDataDictionaryRepositoryImpl extends Repository< UserDataDictionaryMapper , UserDataDictionary> implements UserDataDictionaryRepository {

private final UserDataDictionaryMapper mapper;

public UserDataDictionaryRepositoryImpl(UserDataDictionaryMapper mapper) {
this.mapper = mapper;
}

    /**
    * 搜索用户数据字典关联表
    * @param dto
    * @return
    */
    @Override
    public Page< UserDataDictionary > selectUserDataDictionary(SelectUserDataDictionaryDto dto){

    return mapper.selectUserDataDictionary(dto);
    }

@Override
public  Map<Long, UserDataDictionary> getMap(List<Long> ids){
    Map<Long, UserDataDictionary> map = new HashMap<>();

    if (CollectionUtil.isNotEmpty(ids)) {
    List<UserDataDictionary> list =listByIds(ids);
    if (CollectionUtil.isNotEmpty(list)) {
    map = list.stream().collect(Collectors.toMap(UserDataDictionary::getId, pojo -> pojo));
    }
    }
    return map;
    }

    /**
     * 根据类型和用户id查询用户所关联的
     * @param userId
     * @return
     */
    @Override
    public List<Long> selectByUserIdAndTypeId(Long typeId, Long userId) {
        List<Long> ids =new ArrayList<>();
        List<UserDataDictionary> userDataDictionaries = mapper.selectByUserIdAndTypeId(typeId, userId);
        if (CollectionUtil.isNotEmpty(userDataDictionaries)){
            ids=userDataDictionaries.stream().map(UserDataDictionary::getDataDictionaryId).distinct().collect(Collectors.toList());
        }
        return ids;
    }

    @Override
    public Map<Long, List<UserDataDictionaryDto>> getListByUserIds(List<Long> userIds, Long typeId) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Collections.EMPTY_MAP;
        }

        return mapper.getListByUserIds(userIds, typeId)
                .stream()
                .collect(Collectors.groupingBy(UserDataDictionaryDto::getUserId));

    }
}